package net.wenku8.reader.util

import android.util.Log
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream


fun deleteFile(file : File){
    if (!file.exists()){
        return
    }
    if (file.isDirectory){
        file.listFiles()?.forEach { subFile ->
            deleteFile(subFile)
        }
    }
    file.delete()
}

fun mkParentDirs(file : File) {
    try {
        if(file.parentFile?.exists() == false){
            file.parentFile?.mkdirs()
        }
    } catch (e: Exception) {
        Log.e("", "", e)
    }
}

fun readAsByte(file : File): ByteArray?{
    try {
        return FileInputStream(file).readBytes()
    } catch (e: Exception) {
        Log.e("", "", e)
    }
    return null
}

fun getFileCharset(file : File) : String {
    try {
        val head = ByteArray(3)
        val bis = BufferedInputStream(FileInputStream(file))
        bis.mark(0)
        var read = bis.read(head)
        if (read == -1){
            return "GBK"
        }
        if (head[0] == 0xFF.toByte() && head[1] == 0xFE.toByte()){
            return "UTF-16LE"
        }
        if (head[0] == 0xFE.toByte() && head[1] == 0xFF.toByte()) {
            return "UTF-16BE" //文件编码为 Unicode big endiar
        }
        if (head[0] == 0xEF.toByte() && head[1] == 0xBB.toByte() && head[2] == 0xBF.toByte()) {
            return "UTF-8" //文件编码为 UTF-8
        }
        bis.reset()
        while (true) {
            read = bis.read()
            if (read == -1){
                break
            }
            if (read >= 0xF0){
                return "GBK"
            }
            // 单独出现BF以下的，也算是GBK
            if (read in 0x80..0xBF){
                return "GBK"
            }
            if (read in 0xc0..0xDF) {
                read = bis.read()
                // 双字节(0xC0 - 0XDF) (0x80 - 0xBF)，也可能在GBK编码内
                if (read in 0x80..0xBF){
                    continue
                }else{
                    return "GBK"
                }
            } else if(read in 0xE0..0xEF) {// 也有可能出错，但是几率较小
                read = bis.read()
                if (read in 0x80..0xBF) {
                    read = bis.read()
                    if (read in 0x80..0xBF) {
                        return "UTF-8"
                    }
                }
                break
            }
        }
        bis.close()
    } catch (e: Exception) {
        Log.e("", "", e)
    }
    return "GBK"
}

fun writeStream(input: InputStream, out: OutputStream): Boolean {
    var read: Int  // 读取的字节数
    try {
        val buffer = ByteArray(1024*8)
        while ((input.read(buffer).also { read = it }) != -1) {
            out.write(buffer, 0, read)
        }
        out.flush()
        return true
    } catch (e: Exception) {
        Log.e("", "", e)
    } finally {
        input.close()
        out.close()
    }
    return false
}

fun writeFile(source : File, target : File){
    try {
        writeStream(FileInputStream(source), FileOutputStream(target))
    } catch (e: Exception) {
        Log.e("", "", e)
    }
}
